from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class FavoritesPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.add_favorite_button_locator = (By.ID, "add-favorite-button")
        self.remove_favorite_button_locator = (By.ID, "remove-favorite-button")
        self.favorites_list_locator = (By.ID, "favorites-list")
        self.favorite_product_locator = (By.ID, "favorite-product")
        self.favorite_product_name_locator = (By.ID, "favorite-product-name")
        self.favorite_product_description_locator = (By.ID, "favorite-product-description")
        self.favorite_product_price_locator = (By.ID, "favorite-product-price")
        self.empty_list_message_locator = (By.ID, "empty-list-message")

    def navigate(self):
        self.driver.get('http://example.com/favorites')

    def click_add_favorite(self, product_id):
        add_favorite_button = (By.ID, f"add-favorite-{product_id}")
        add_favorite_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(add_favorite_button)
        )
        add_favorite_button.click()

    def click_remove_favorite(self, product_id):
        remove_favorite_button = (By.ID, f"remove-favorite-{product_id}")
        remove_favorite_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(remove_favorite_button)
        )
        remove_favorite_button.click()

    def get_favorites_list(self):
        favorites_list = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.favorites_list_locator)
        )
        return favorites_list

    def get_favorite_product(self, product_id):
        favorite_product = (By.ID, f"favorite-product-{product_id}")
        favorite_product = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(favorite_product)
        )
        return favorite_product

    def get_favorite_product_name(self, product_id):
        favorite_product_name = (By.ID, f"favorite-product-name-{product_id}")
        favorite_product_name = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(favorite_product_name)
        )
        return favorite_product_name.text

    def get_favorite_product_description(self, product_id):
        favorite_product_description = (By.ID, f"favorite-product-description-{product_id}")
        favorite_product_description = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(favorite_product_description)
        )
        return favorite_product_description.text

    def get_favorite_product_price(self, product_id):
        favorite_product_price = (By.ID, f"favorite-product-price-{product_id}")
        favorite_product_price = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(favorite_product_price)
        )
        return favorite_product_price.text

    def get_empty_list_message(self):
        empty_list_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.empty_list_message_locator)
        )
        return empty_list_message.text